﻿using System;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ControlExplorer.samples.Paging
{
  public partial class Overview : SpreadDemoPage
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      Label7.Visible = false;
      if (IsPostBack) return;

      this.FpSpread1.UseClipboard = false;
      FpSpread1.ActiveSheetView.AllowDelete = true;
      FpSpread1.ActiveSheetView.AllowInsert = true;

      FpSpread1.DataSource = GetSuppliers();
      Page.DataBind();

      FpSpread1.ActiveSheetView.Columns[0].Label = "ID";
      FpSpread1.ActiveSheetView.Columns[1].Label = "公司名称";
      FpSpread1.ActiveSheetView.Columns[2].Label = "联系人";
      FpSpread1.ActiveSheetView.Columns[3].Label = "主题";

      FpSpread1.ActiveSheetView.Columns[0].Width = 40;
      FpSpread1.ActiveSheetView.Columns[1].Width = 150;
      FpSpread1.ActiveSheetView.Columns[2].Width = 150;
      FpSpread1.ActiveSheetView.Columns[3].Width = 150;
      FpSpread1.ActiveSheetView.Columns[4].Width = 150;

      FpSpread1.Pager.Position = FarPoint.Web.Spread.PagerPosition.TopCommandBar;
      FpSpread1.Pager.Mode = FarPoint.Web.Spread.PagerMode.Both;
      FpSpread1.Pager.BackColor = Color.Beige;
      FpSpread1.Pager.Align = HorizontalAlign.Right;

      FpSpread1.CommandBar.BackColor = Color.Linen;

      FpSpread1.ActiveSheetView.PageSize = 6;
      FpSpread1.Pager.Align = HorizontalAlign.Right;
      FpSpread1.Pager.BackColor = Color.LightBlue;
      FpSpread1.Pager.Position = FarPoint.Web.Spread.PagerPosition.TopCommandBar;

      InitCombos();
    }

    public void InitCombos()
    {
      int i;

      //Init the combox values
      cbMode.Items.Add("导航");
      cbMode.Items.Add("数字");
      cbMode.Items.Add("导航+数字");
      cbMode.SelectedIndex = 2;

      cbAlign.Items.Add("默认值");
      cbAlign.Items.Add("左对齐");
      cbAlign.Items.Add("居中");
      cbAlign.Items.Add("右对齐");
      cbAlign.Items.Add("两端对齐");
      cbAlign.SelectedIndex = 3;

      for (i = 1; i <= 10; i++)
      {
        //10 pages
        cbPageCnt.Items.Add(i.ToString());
      }
      cbPageCnt.SelectedIndex = 5;

      cbPosition.Items.Add("顶部");
      cbPosition.Items.Add("底部");
      cbPosition.Items.Add("工具条内部");
      cbPosition.Items.Add("顶部和底部");
      cbPosition.Items.Add("顶部工具条内");
      cbPosition.SelectedIndex = 4;
    }

    protected void btnLoad_Click(object sender, System.EventArgs e)
    {
      Label7.Visible = false;
      try
      {
        FpSpread1.Pager.Position = (FarPoint.Web.Spread.PagerPosition)cbPosition.SelectedIndex;
        FpSpread1.Pager.Mode = (FarPoint.Web.Spread.PagerMode)cbMode.SelectedIndex;
        FpSpread1.Pager.Align = (HorizontalAlign)cbAlign.SelectedIndex;
        FpSpread1.ActiveSheetView.PageSize = cbPageCnt.SelectedIndex +1;
      }

      catch (Exception ex)
      {
        Label7.Visible = true;
        //Reset selection to Next/Previous
        cbMode.SelectedIndex = 0;

        //Set page size
        FpSpread1.Sheets[0].PageSize = cbPageCnt.SelectedIndex + 1;
      }
    }

    DataSet GetSuppliers()
    {
      string constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Northwind.mdb;Persist Security Info=True";
      string sqlstr =
@"SELECT TOP 23 
        SupplierID, 
        CompanyName, 
        ContactName, 
        ContactTitle, 
        Address as 地址, 
        City as 城市, 
        Region as 区域, 
        PostalCode as 邮政编码, 
        Country as 国家, 
        Phone as 电话, 
        Fax as 传真, 
        HomePage as 主页
  FROM suppliers";

      DataSet suppliers = new DataSet();
      using (OleDbConnection myconn = new OleDbConnection(constr))
      {
        OleDbDataAdapter sqlAdapter1 = new OleDbDataAdapter(sqlstr, myconn);
        sqlAdapter1.Fill(suppliers, "Suppliers");
      }

      return suppliers;
    }
  }
}